package com.example.myapplication.login;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.myapplication.R;
import com.example.myapplication.acticity.BaseActivity;
import com.example.myapplication.api.Api;
import com.example.myapplication.api.AppConfig;
import com.example.myapplication.api.TitleCallBack;

import java.util.HashMap;

public class RegisterActivity extends BaseActivity {

    private EditText etAcc, etPsw;
    private Button btRegister;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        initView();
    }

    @Override
    public void publish(int progress) {

    }

    @Override
    public void change(int position) {

    }

    private void initView() {

        etAcc = findViewById(R.id.et_acc);
        etPsw = findViewById(R.id.et_pwd);
        btRegister = findViewById(R.id.btn_register);
        //获取输入框的内容并去除前后的空格
        btRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String acc = etAcc.getText().toString().trim();
                String psw = etPsw.getText().toString().trim();
                register(acc, psw);
            }
        });
    }

    /**
     * 登录的操作
     *
     * @param acc
     * @param psw
     */
    private void register(String acc, String psw) {

        if (acc.isEmpty()) {
            showToast("账号不能为空！");
        } else if (psw.isEmpty()) {
            showToast("密码不能为空！");
        } else {
            if (acc.isEmpty()) {
                showToast("账号不能为空！");
            } else if (psw.isEmpty()) {
                showToast("密码不能为空！");
            } else {

                HashMap<String, Object> params = new HashMap<>();
                params.put("mobile", acc);
                params.put("password", psw);
                Api.config(AppConfig.REGSTER, params).postRequest(new TitleCallBack() {
                    @Override
                    public void onSuccess(final String res) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                showToast(res);
                                showToast("注册成功");
                            }
                        });
                    }

                    @Override
                    public void onFailure(Exception e) {
                        showToast("注册失败");
                    }
                });
            }

        }
    }
}
